package com.drug.mingkang.service.impl;
import com.drug.mingkang.mapper.HqRawmaterialTypeMapper;
import com.drug.mingkang.model.HqRawmaterialType;
import com.drug.mingkang.service.HqRawmaterialTypeService;
import com.drug.mingkang.utils.ProductTypeParent;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (HqRawmaterialType)表服务实现类
 *
 * @author ll
 * @since 2020-08-10 15:19:45
 */
@Service("hqRawmaterialTypeService")
public class HqRawmaterialTypeServiceImpl implements HqRawmaterialTypeService {
    @Resource
    private HqRawmaterialTypeMapper hqRawmaterialTypeMapper;

    /**
     * 通过ID查询单条数据
     *
     * @param typeId 主键
     * @return 实例对象
     */
    @Override
    public HqRawmaterialType queryById(String typeId) {
        return this.hqRawmaterialTypeMapper.queryById(typeId);
    }

    /**
     * 查询多条数据
     * @return 对象列表
     */
    @Override
    public List<HqRawmaterialType> queryAllByLimit(HqRawmaterialType type) {
        return this.hqRawmaterialTypeMapper.queryAllByLimit(type);
    }

    /**
     * 新增数据
     *
     * @param hqRawmaterialType 实例对象
     * @return 实例对象
     */
    @Override
    public int insert(HqRawmaterialType hqRawmaterialType) {
        int rows =  this.hqRawmaterialTypeMapper.insert(hqRawmaterialType);
        return rows;
    }

    /**
     * 修改数据
     * @param hqRawmaterialType 实例对象
     * @return 实例对象
     */
    @Override
    public int update(HqRawmaterialType hqRawmaterialType) {
        int rows = this.hqRawmaterialTypeMapper.update(hqRawmaterialType);
        return rows;
    }

    /**
     * 通过主键删除数据
     *
     * @param typeId 主键
     * @return 是否成功
     */
    @Override
    public int deleteById(String typeId) {
        int rows = this.hqRawmaterialTypeMapper.deleteById(typeId);
        return  rows ;
    }

    @Override
    public List<HqRawmaterialType> queryAll(HqRawmaterialType hqRawmaterialType) {
        return this.hqRawmaterialTypeMapper.queryAll(hqRawmaterialType);
    }

    @Override
    public int selectCount(HqRawmaterialType type) {
        return this.hqRawmaterialTypeMapper.selectCount(type);
    }

    @Override
    public HqRawmaterialType selectName(HqRawmaterialType hqRawmaterialType) {
        return this.hqRawmaterialTypeMapper.selectName(hqRawmaterialType);
    }


    @Override
    public List<ProductTypeParent> getTree() {
        List<ProductTypeParent> list = getNodeTree("0");
        ProductTypeParent root = new ProductTypeParent();
        root.setId("root");
        root.setLabel("全部");
        root.setChildren(list);
        List<ProductTypeParent> listAll = new ArrayList<>();
        listAll.add(root);
        return listAll;
    }

    @Override
    public List<HqRawmaterialType> selectDlt(String typeId) {
        return  this.hqRawmaterialTypeMapper.selectDlt(typeId);
    }


    /**
     * 根据类型顶级节点id查询所有子类型
     * @param typeId
     * @return
     */
    public List<ProductTypeParent> getNodeTree(String typeId){
        List<ProductTypeParent> list = new ArrayList<>();
        List<HqRawmaterialType> node = hqRawmaterialTypeMapper.findNode(typeId);
        for (HqRawmaterialType h:node) {
            ProductTypeParent parent = new ProductTypeParent();
            parent.setId(h.getTypeId());
            parent.setLabel(h.getTypeName());
            parent.setChildren(getNodeTree(h.getTypeId()));
            list.add(parent);
        }
        return list;
    }

}